package com.example.liliang.viewlinstenertest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ButtonActivity extends AppCompatActivity {

    private Button btn, btn1;
    private ButtonListener buttonListener;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_linearlayout);
        btn = findViewById(R.id.btn);
        btn1 = findViewById(R.id.btn1);
        buttonListener = new ButtonListener();
        btn.setOnClickListener(buttonListener);
        btn1.setOnClickListener(buttonListener);

        // 不现实  浪费资源
//        btn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Toast.makeText(ButtonActivity.this,"dsfdsf",Toast.LENGTH_SHORT).show();
//            }
//        });
//        btn1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Toast.makeText(ButtonActivity.this,"dsfdsf",Toast.LENGTH_SHORT).show();
//            }
//        });
    }

    class ButtonListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn:
                    Toast.makeText(ButtonActivity.this, ((Button)view).getText().toString(), Toast.LENGTH_SHORT).show();
                    break;
                case R.id.btn1:
                    Toast.makeText(ButtonActivity.this, "按钮2", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    }
}
